﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Gameplay.PVE
{
    [CustomEditor(typeof(RpgShadow2D))]
    public class RpgShadow2DEditor : UnityEditor.Editor
    {
        private Vector3 forward;
        private Material shadowMaterial;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            var obj = target as RpgShadow2D;
            var transform = obj.transform;
            if (GUILayout.Button("添加阴影"))
            {
                if (shadowMaterial == null)
                {
                    shadowMaterial = Material.Instantiate(AssetDatabase.LoadAssetAtPath<Material>(
                        "Assets/ResourcesAssets/Pve/Materials/mat_model_shadow.mat"));
                }
                var parent = transform.parent;
                var modelHitPoints = parent.gameObject.GetComponentsInChildren<ModelHitPoint>();
                for (int i = 0; i < modelHitPoints.Length; i++)
                {
                    modelHitPoints[i].InitShadow(shadowMaterial);
                    modelHitPoints[i].SetShadowVisible(true);
                }
                
                var shadowOffset = shadowMaterial.GetVector("_ShadowOffset");
                obj.x = shadowOffset.x;
                obj.y = shadowOffset.y;
                obj.z = shadowOffset.z;
                obj.w = shadowOffset.w;
                var color = shadowMaterial.GetColor("_ShadowColor");
                obj.shadowColor = color;
                var height = shadowMaterial.GetFloat("_ShadowHeight");
                obj.shadowHeight = height;
            }

            if (shadowMaterial)
            {
                shadowMaterial.SetVector("_ShadowOffset", new Vector4(obj.x, obj.y, obj.z, obj.w));
                shadowMaterial.SetColor("_ShadowColor", obj.shadowColor);
                shadowMaterial.SetFloat("_ShadowHeight", obj.shadowHeight);
            }
        }
    }
}